package com.ruoyi.system.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PlatformPlan;
import com.ruoyi.system.service.IPlatformPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仿真计划管理Controller
 * 
 * @author zzkluck
 * @date 2025-02-12
 */
@RestController
@RequestMapping("/system/plan")
public class PlatformPlanController extends BaseController
{
    @Autowired
    private IPlatformPlanService platformPlanService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    /**
     * 查询仿真计划管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlatformPlan platformPlan)
    {
        startPage();
        List<PlatformPlan> list = platformPlanService.selectPlatformPlanList(platformPlan);
        return getDataTable(list);
    }

    /**
     * 导出仿真计划管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:plan:export')")
    @Log(title = "仿真计划管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlatformPlan platformPlan)
    {
        List<PlatformPlan> list = platformPlanService.selectPlatformPlanList(platformPlan);
        ExcelUtil<PlatformPlan> util = new ExcelUtil<PlatformPlan>(PlatformPlan.class);
        util.exportExcel(response, list, "仿真计划管理数据");
    }

    /**
     * 获取仿真计划管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(platformPlanService.selectPlatformPlanById(id));
    }

    /**
     * 新增仿真计划管理
     */
    @PreAuthorize("@ss.hasPermi('system:plan:add')")
    @Log(title = "仿真计划管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformPlan platformPlan)
    {
        return toAjax(platformPlanService.insertPlatformPlan(platformPlan));
    }

    /**
     * 修改仿真计划管理
     */
    @PreAuthorize("@ss.hasPermi('system:plan:edit')")
    @Log(title = "仿真计划管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformPlan platformPlan)
    {
        return toAjax(platformPlanService.updatePlatformPlan(platformPlan));
    }

    /**
     * 删除仿真计划管理
     */
    @PreAuthorize("@ss.hasPermi('system:plan:remove')")
    @Log(title = "仿真计划管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(platformPlanService.deletePlatformPlanByIds(ids));
    }

    /**
     * 计划审批
     */
    @ApiOperation("订单审批")
    @GetMapping("/audit")
    @ResponseBody
    public AjaxResult finance(String taskid)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            PlatformPlan apply = platformPlanService.selectPlatformPlanById(Long.parseLong(p.getBusinessKey()));
            return AjaxResult.success(apply);
        }
        return AjaxResult.error("流程不存在");
    }
}
